/*
 * CustomerEditInfo.java
 *
 * Created on Feb 7, 2011, 4:52:10 PM
 */
package edu.rit.varapp.graphical;

import java.util.Arrays;
import edu.rit.varapp.datalayer.Customer;
import edu.rit.varapp.datalayer.CustomerOps;
import edu.rit.varapp.utilities.ExceptionUtils;
import java.awt.Dimension;
import java.sql.SQLException;
import javax.swing.JOptionPane;

import static edu.rit.varapp.utilities.ValidationUtils.*;
import static edu.rit.varapp.utilities.StringUtils.emptyString;
/**
 *
 * @author Eric Kisner
 */
public class CustomerEditInfo extends AbstractView {

    private final CustomerOps cOps = new CustomerOps();
    private int entityId;
    private static final int VIEW_WIDTH = 385;
    private static final int VIEW_HEIGHT = 432;

    /** Creates new form CustomerEditInfo */
    public CustomerEditInfo( int entityId ) {
        super( new Dimension( VIEW_WIDTH, VIEW_HEIGHT ) );
        initComponents();
        this.entityId = entityId;
        showData( entityId );
    }

    /**
     * Adds the JTable to the content pane if there are no errors.
     */
    private void showData( int entityId ) {
        try {
            Customer customer = cOps.retrieveCustomer( entityId );
            contactNameField.setText( customer.getContactName() );
            companyNameField.setText( customer.getCompanyName() );
            titleField.setText( customer.getTitle() );
            stateField.setText( customer.getState() );
            zipcodeField.setText( customer.getZipcode() );
            address2Field.setText( customer.getAddr2() );
            address1Field.setText( customer.getAddr1() );
            cityField.setText( customer.getCity() );
            emailField.setText( customer.getEmail() );
        } catch( SQLException sqle ) {
            ExceptionUtils.handleException( this, sqle, "An error has occurred when trying to access your information."
                                                        + "\nPlease ask an administrator to check the log files for "
                                                        + "more details.",
                                            this.getClass() );
            System.exit( 1 );
        }
    }

    /**
     * Validates any input for the text fields of this GUI.
     * @return True if all fields are valid, else false.
     */
    private boolean validateInput() {
        String[] methodNames = {
            "VarCharLength", "Title", "Name", "Email", "Char", "State"
        };
        String[] messages = {
            "Company Name", "Title (ex. Mr/Ms)", "Contact Name (ex. John Smith)",
            "Email Address (test@test.com)", "City", "State (ex. NY)"
        };
        String[] values = {
            companyNameField.getText(), titleField.getText(), contactNameField.getText(),
            emailField.getText(), cityField.getText(), stateField.getText()
        };

        String errorMessage = runValidateMethodList( Arrays.asList( methodNames ),
                                                     Arrays.asList( messages ),
                                                     Arrays.asList( values ) );
        if( errorMessage.equals( emptyString() ) ) {
            return true;
        } else {
            JOptionPane.showMessageDialog( rootPane, errorMessage );
            return false;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        applyButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        usernameLabel4 = new javax.swing.JLabel();
        titleField = new javax.swing.JTextField();
        usernameLabel = new javax.swing.JLabel();
        contactNameField = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        companyNameField = new javax.swing.JTextField();
        serverLabel = new javax.swing.JLabel();
        address1Field = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        address2Field = new javax.swing.JTextField();
        usernameLabel1 = new javax.swing.JLabel();
        cityField = new javax.swing.JTextField();
        usernameLabel2 = new javax.swing.JLabel();
        stateField = new javax.swing.JTextField();
        usernameLabel3 = new javax.swing.JLabel();
        zipcodeField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        emailField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Edit Customer Information");
        setMinimumSize(new java.awt.Dimension(385, 432));
        setPreferredSize(new java.awt.Dimension(385, 432));
        setResizable(false);
        setSize(new java.awt.Dimension(385, 432));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        getContentPane().setLayout(null);

        applyButton.setText("Apply Changes");
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });
        getContentPane().add(applyButton);
        applyButton.setBounds(60, 370, 130, 29);

        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        getContentPane().add(backButton);
        backButton.setBounds(200, 370, 130, 29);

        jPanel1.setLayout(new java.awt.GridLayout(9, 2, 5, 5));

        usernameLabel4.setText("Title:");
        jPanel1.add(usernameLabel4);
        jPanel1.add(titleField);

        usernameLabel.setText("Name:");
        jPanel1.add(usernameLabel);
        jPanel1.add(contactNameField);

        passwordLabel.setText("Company:");
        jPanel1.add(passwordLabel);
        jPanel1.add(companyNameField);

        serverLabel.setText("Address 1:");
        jPanel1.add(serverLabel);
        jPanel1.add(address1Field);

        jLabel2.setText("Address 2:");
        jPanel1.add(jLabel2);
        jPanel1.add(address2Field);

        usernameLabel1.setText("City:");
        jPanel1.add(usernameLabel1);
        jPanel1.add(cityField);

        usernameLabel2.setText("State:");
        jPanel1.add(usernameLabel2);
        jPanel1.add(stateField);

        usernameLabel3.setText("Zipcode:");
        jPanel1.add(usernameLabel3);
        jPanel1.add(zipcodeField);

        jLabel3.setText("Email:");
        jPanel1.add(jLabel3);
        jPanel1.add(emailField);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(35, 60, 310, 300);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Customer Information");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(90, 20, 190, 30);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_backButtonActionPerformed

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed
        if (validateInput()) {
            String contactname = contactNameField.getText();
            String companyname = companyNameField.getText();
            String title = titleField.getText();
            String email = emailField.getText();
            String address1 = address1Field.getText();
            String address2 = address2Field.getText();
            String city = cityField.getText();
            String state = stateField.getText();
            String zipcode = zipcodeField.getText();

            // create new customer object
            Customer customer = new Customer( entityId, title, contactname, companyname, address1,
                                              address2, city, state, zipcode, email );

            try {
                cOps.updateCustomer( customer, entityId );
                JOptionPane.showMessageDialog( rootPane, "Successfully updated your information." );
            } catch( SQLException sqle ) {
                ExceptionUtils.handleException( this, sqle, "There was a problem updating your customer information."
                                                            + "\nPlease ask an administrator to check the log files for "
                                                            + "more details.", this.getClass() );
                System.exit( 1 );
            }
        }
    }//GEN-LAST:event_applyButtonActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        new CustomerOperations( entityId ).setVisible( true );
    }//GEN-LAST:event_formWindowClosed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField address1Field;
    private javax.swing.JTextField address2Field;
    private javax.swing.JButton applyButton;
    private javax.swing.JButton backButton;
    private javax.swing.JTextField cityField;
    private javax.swing.JTextField companyNameField;
    private javax.swing.JTextField contactNameField;
    private javax.swing.JTextField emailField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel serverLabel;
    private javax.swing.JTextField stateField;
    private javax.swing.JTextField titleField;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JLabel usernameLabel1;
    private javax.swing.JLabel usernameLabel2;
    private javax.swing.JLabel usernameLabel3;
    private javax.swing.JLabel usernameLabel4;
    private javax.swing.JTextField zipcodeField;
    // End of variables declaration//GEN-END:variables
}
